﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

using Game.Common;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;
using Game.Surface;
using Game.Toolkit;
using Game.Types;

namespace Game.Editor.Toolbox
{
    public class BuildingItemControl2 : UserControl
    {
        readonly Canvas _Canvas = new Canvas();
        readonly ViewSurfaceControl _SurfaceControl;

        Button _Button;
        GameObjectGroup _GameObjectGroup;

        public BuildingItemControl2()
        {
            InitializeComponent();

            IGameMap map = Factory.Create(4, 4);
            _SurfaceControl = new ViewSurfaceControl(map) {GameScale = 0.50};
            _Canvas.Children.Add(_SurfaceControl);

            Canvas.SetLeft(_SurfaceControl, -150);
            Canvas.SetTop(_SurfaceControl, -100);
        }

        void InitializeComponent()
        {
            Grid grid = new Grid();
            Content = grid;

            _Button = new Button();
            grid.Children.Add(_Button);

            _Button.Click += OnClick;
            _Button.ClickMode = ClickMode.Press;
            _Button.Content = _Canvas;
        }

        public GameObjectGroup GameObjectGroup
        {
            get { return _GameObjectGroup; }
            set
            {
                _GameObjectGroup = value;
                DisplayType();
            }
        }

        void OnClick(object source, EventArgs e)
        {
            if (_GameObjectGroup != null)
            {
                SelectedBuilding.GameObjectGroup = _GameObjectGroup;
            }

            SelectedGameObjectGroup.GameObjectGroup = null;
        }

        void DisplayType()
        {
            if (_GameObjectGroup != null)
            {
                DisplayBuildingObject();

                ToolTip t = new ToolTip();
                t.Content = "" + _GameObjectGroup.Name;
                ToolTipService.SetToolTip(_Button, t);
            }
        }

        void DisplayBuildingObject()
        {
            List<GameObjectGroupPart> parts = _GameObjectGroup.Parts;

            foreach (GameObjectGroupPart part in parts)
            {
                AssignObject(part.ObjectType,
                    part.GameDirection,
                    new Point(0, 0),
                    _SurfaceControl);
            }
        }

        static void AssignObject(
            ObjectType item,
            GameDirection offset,
            Point location,
            SurfaceControl surfaceControl)
        {
            double dx = location.X;
            double dy = location.Y;
            dx += offset.Value.X;
            dy += offset.Value.Y;

            if ((dx % 2) == 1)
            {
                dy += 1;
            }

            var origin = new Point(dx, dy);
            var field = MapUtil.GetField(
                surfaceControl.GameMapView,
                origin, ObjectOrientation.Center);

            if (field != null)
            {
                field.ObjectX().GameObject = new GameObject(item);
            }
        }

    }
}
